/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2020-11-13 10:21
**/
package models

import (
	mydb  "ydd-im/service/dbproxy/util/mysql/conn"
	"database/sql"
	"fmt"
	"time"
)
type UserToken struct {
	Id       int         `db:"id"`
	Userid 	 int    	 `db:"userid"`
	Username string      `db:"username"`
	Token    string      `db:"token"` //创建时间
	LoginTime  time.Time `db:"login_time"`
	ExpireTime  time.Time `db:"expiretime"`
	ClientType string    `db:"client_type"`
	ClientId   string    `db:"clientId"`
	State   string    	 `db:"state"`
}

//orm 回调
func (u *UserToken) TableName() string {
	return TNUsertoken()
}

func NewUserToken() *UserToken {
	return &UserToken{}
}
// 插入token信息
func (u *UserToken) AddUserToken(usertoken *UserToken) (insertid int64, err error) {
	sqlStr := "insert ignore into " + u.TableName() + "(userid, username,token,login_time,client_type,clientId,expiretime) values (?,?,?,?,?,?,?)"
	dbconn,err := mydb.DBConn("w")
	if err != nil {
		return 0, err
	}
	ret, err := dbconn.Exec(sqlStr, usertoken.Userid,usertoken.Username,usertoken.Token,usertoken.LoginTime,usertoken.ClientType,usertoken.ClientId,usertoken.ExpireTime)
	fmt.Println(ret)
	if err != nil {
		return 0, err
	}
	theID, err := ret.LastInsertId() // 新插入数据的id
	if err != nil {
		return 0, err
	}
	return theID, nil
}
// 根据uid,client_type查询用户信息
func (u *UserToken) GetUserTokenInfo(uid int,client_type string) (usertoken UserToken, err error) {
	usertoken = UserToken{}
	sqlStr := "select id,userid,username,token,login_time,client_type,clientId,expiretime from " + u.TableName() + " where userid=? and client_type = ?"
	dbconn,err := mydb.DBConn("r")
	if err != nil {
		return usertoken, err
	}
	err = dbconn.Get(&usertoken, sqlStr, uid,client_type)
	if err == sql.ErrNoRows { // 为空
		return usertoken, nil
	}
	if err != nil {
		return usertoken, err
	}
	return usertoken, nil
}
// 根据有效的token查询用户信息
func (u *UserToken) GetUserInfoByToken(token string) (user User, err error) {
	us := &User{}
	sqlStr := "select a.id,a.username,a.passwd,a.avatar,a.sex,a.nickname,a.salt,a.online,a.memo,a.createat,a.email,a.sid,b.clientId from " + us.TableName() + " as a inner join " + u.TableName() + " as b on a.id =b.userid  where b.token=?  and b.expiretime>?"
	dbconn,err := mydb.DBConn("r")
	if err != nil {
		return user, err
	}
	err = dbconn.Get(&user, sqlStr, token, time.Now().Format("2006-01-02 15:04:05"))
	if err == sql.ErrNoRows { // 为空
		return user, nil
	}
	if err != nil {
		return user, err
	}
	return user, nil
}
// 根据有效的token查询用户信息
func (u *UserToken) UpdateUserInfoByToken(token,clientid string) (err error) {
	sqlStr := "update "+ u.TableName() + " set clientid=?,state = '1' where token = ?" // 更新地址，并且标志在线
	dbconn,err := mydb.DBConn("w")
	if err != nil {
		return  err
	}
	ret, err := dbconn.Exec(sqlStr, clientid, token)
	if err != nil {
		return err
	}
	_, err = ret.RowsAffected() // 操作影响的行数
	if err != nil {
		return err
	}
	return nil
}
// 根据有效的clientid,userid修改状态
func (u *UserToken) UpdateUserTokenState(clientid string,userid int ,state string) (err error) {
	sqlStr := "update "+ u.TableName() + " set state = ? where clientid = ? and  userid = ?" // 更新地址，并且标志在线
	dbconn,err := mydb.DBConn("w")
	if err != nil {
		return  err
	}
	ret, err := dbconn.Exec(sqlStr,state, clientid, userid)
	if err != nil {
		return err
	}
	_, err = ret.RowsAffected() // 操作影响的行数
	if err != nil {
		return err
	}
	return nil
}
// 根据uid,client_type查询用户信息
func (u *UserToken) GetUserTokenList(touid,uid int) (usertoken []UserToken, err error) {
	usertoken = []UserToken{}
	us := &User{}
	sqlStr := "select a.id,a.userid,a.username,a.token,a.login_time,a.client_type,a.clientId,a.expiretime from " + u.TableName() + " as a inner join "+us.TableName()+" as b on a.userid = b.id  where (a.userid=? or a.userid = ?) and a.expiretime > ? and a.clientId !=''"
	dbconn,err := mydb.DBConn("r")
	if err != nil {
		return  usertoken,err
	}
	err = dbconn.Select(&usertoken, sqlStr, uid,touid, time.Now().Format("2006-01-02 15:04:05"))
	if err == sql.ErrNoRows { // 为空
		return usertoken, nil
	}
	if err != nil {
		return usertoken, err
	}
	return usertoken, nil
}

// 根据uid,client_type查询用户信息
func (u *UserToken) GetUserTokenByTouidList(touid int) (usertoken []UserToken, err error) {
	usertoken = []UserToken{}
	us := &User{}
	sqlStr := "select a.id,a.userid,a.username,a.token,a.login_time,a.client_type,a.clientId,a.expiretime from " + u.TableName() + " as a inner join "+us.TableName()+" as b on a.userid = b.id  where (a.userid=? ) and a.expiretime > ? and a.clientId !=''"
	dbconn,err := mydb.DBConn("r")
	if err != nil {
		return  usertoken,err
	}
	err = dbconn.Select(&usertoken, sqlStr,touid, time.Now().Format("2006-01-02 15:04:05"))
	if err == sql.ErrNoRows { // 为空
		return usertoken, nil
	}
	if err != nil {
		return usertoken, err
	}
	return usertoken, nil
}